/**
 * 
 */
package com.dalonedrau.jogl.opengl.ui.panel;

import com.dalonedrau.jogl.engine.Action;
import com.dalonedrau.jogl.engine.InputEvent;
import com.dalonedrau.jogl.engine.JOGLErrorHandler;
import com.dalonedrau.jogl.engine.ProjectConstants;
import com.dalonedrau.jogl.opengl.sprite.SpriteImageObjectFactory;
import com.dalonedrau.jogl.opengl.ui.Button;
import com.dalonedrau.jogl.opengl.ui.SimpleScrollingTextWindow;
import com.dalonedrau.jogl.opengl.ui.UIFactory;
import com.dalonedrau.pool.PooledStringBuilder;
import com.dalonedrau.pool.StringBuilderPool;

/**
 * @author Donald
 */
public final class SimpleScrollingTextPanel extends LandscapePagePanel {
	/** the button to move down one line of text. */
	private Button						btnLineDown;
	/** the button to move up one line of text. */
	private Button						btnLineUp;
	/** the previous last line displayed. */
	private int							prevCurrentLine;
	/** the {@link SimpleScrollingTextWindow}. */
	private SimpleScrollingTextWindow	tw;
	/**
	 * Creates a new instance of {@link SimpleScrollingTextPanel}.
	 * @param font the name of the font used
	 * @param maxLines the maximum number of lines of text displayed
	 * @param maxChars the width in characters of the text window
	 */
	public SimpleScrollingTextPanel(final String font,
			final int maxLines, final int maxChars) {
		this(font, maxLines, maxChars, false);
	}
	/**
	 * Creates a new instance of {@link SimpleScrollingTextPanel}.
	 * @param font the name of the font used
	 * @param maxLines the maximum number of lines of text displayed
	 * @param maxChars the width in characters of the text window
	 * @param debug if true, debugging is turned on
	 */
	public SimpleScrollingTextPanel(final String font,
			final int maxLines, final int maxChars, final boolean debug) {
		try {
			final SimpleScrollingTextPanel me = this;
			tw = new SimpleScrollingTextWindow(
					font, "", maxLines, maxChars, debug);
			int img = SpriteImageObjectFactory.getInstance().getImageRefId(
					ProjectConstants.getInstance().getScrollBarUpArrowImage());
			btnLineUp = UIFactory.getInstance().getStandardImageButton(img);
			// btnLineUp.setTooltipText("Previous Line");
			btnLineUp.addAction(new Action() {
				/**
				 * {@inheritDoc}
				 */
				@Override
				public void performAction() {
					try {
						InputEvent.getInstance().addAction(
								me, // object
								me.getClass()
										.getMethod(
												"scroll",
												new Class[] { boolean.class }),
								new Boolean[] { true }
								);
					} catch (Exception ex) {
						JOGLErrorHandler.getInstance().fatalError(ex);
					}
				}
			});
			img = SpriteImageObjectFactory.getInstance()
					.getImageRefId(
							ProjectConstants.getInstance()
									.getScrollBarDownArrowImage());
			btnLineDown = UIFactory.getInstance().getStandardImageButton(img);
			// btnLineDown.setTooltipText("Next Line");
			btnLineDown.addAction(new Action() {
				/**
				 * {@inheritDoc}
				 */
				@Override
				public void performAction() {
					try {
						InputEvent.getInstance().addAction(
								me, // object
								me.getClass()
										.getMethod(
												"scroll",
												new Class[] { boolean.class }),
								new Boolean[] { false }
								);
					} catch (Exception ex) {
						JOGLErrorHandler.getInstance().fatalError(ex);
					}
				}
			});
			super.addChild(tw);
			Panel pnl = new PortraitPagePanel(Panel.PORTRAIT_CENTER);
			pnl.addChild(btnLineUp);
			int spacer = tw.getTextWindowHeight();
			spacer -= btnLineUp.getPreferredSize().height;
			spacer -= btnLineDown.getPreferredSize().height;
			pnl.addChild(UIFactory.getInstance().getSpacer(1, spacer));
			pnl.addChild(btnLineDown);
			super.addChild(pnl);
		} catch (Exception ex) {
			JOGLErrorHandler.getInstance().fatalError(ex);
		}
	}
	private boolean canScrollDown() {
		boolean moreDown = true;
		if (tw.getCurrentLine() >= tw.getNumberOfLinesOfText() - 1) {
			moreDown = false;
		}
		return moreDown;
	}
	private boolean canScrollUp() {
		boolean moreUp = true;
		if (tw.getCurrentLine() == 0) {
			moreUp = false;
		} else {
			if (tw.getCurrentLine() - tw.getNumLinesDisplayed() < 0) {
				moreUp = false;
			}
		}
		return moreUp;
	}
	/**
	 * Attempts to scroll the text.
	 * @param up if true; the text is scrolled up; otherwise text is scrolled
	 *        down
	 */
	public void scroll(final boolean up) {
		if (up && canScrollUp()) {
			tw.setCurrentLine(tw.getCurrentLine() - 1);
		} else if (!up && canScrollDown()) {
			tw.setCurrentLine(tw.getCurrentLine() + 1);
			if (tw.getCurrentLine() > prevCurrentLine) {
				prevCurrentLine = tw.getCurrentLine();
			}
		}
		setIcons();
	}
	private void setIcons() {
		try {
			btnLineUp.setActive(false);
			PooledStringBuilder sb =
					StringBuilderPool.getInstance().getStringBuilder();
			sb.append(
					ProjectConstants.getInstance().getScrollBarUpArrowImage());
			if (canScrollUp()) {
				sb.append("_more");
				btnLineUp.setActive(true);
			}
			int img = SpriteImageObjectFactory.getInstance().getImageRefId(
					sb.toString());
			btnLineUp.getSprite().setImageRefId(img);
			sb.setLength(0);

			btnLineDown.setActive(false);
			sb.append(
					ProjectConstants.getInstance()
							.getScrollBarDownArrowImage());
			if (canScrollDown()) {
				sb.append("_more");
				btnLineDown.setActive(true);
			}
			if (prevCurrentLine < tw.getNumberOfLinesOfText() - 1) {
				sb.append("_more");
			}
			img = SpriteImageObjectFactory.getInstance().getImageRefId(
					sb.toString());
			btnLineDown.getSprite().setImageRefId(img);
			sb.setLength(0);
			sb.returnToPool();
		} catch (Exception ex) {
			JOGLErrorHandler.getInstance().fatalError(ex);
		}
	}
	/**
	 * Updates the text displayed.
	 * @param text the new text
	 * @throws Exception if an error occurs
	 */
	public void updateText(final String text) throws Exception {
		System.out.println("stp.updatetext(" + text);
		tw.updateText(text);
		int newBottom = prevCurrentLine + tw.getNumLinesDisplayed() - 1;
		if (newBottom >= tw.getNumberOfLinesOfText()) {
			newBottom = tw.getNumberOfLinesOfText() - 1;
		}
		prevCurrentLine = newBottom;
		tw.setCurrentLine(newBottom);
		setIcons();
	}
}
